<?php

namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
use think\facade\Request;

class Shop extends Base
{
    # 商品列表
    public function goodslists()
    {
        //$type = input('post.type',1);
        $where = [];

        // 搜索
        // if (!empty($value)) {
        //     if ($type == 1) {
        //         $where[] = ['test_input', 'like', '%' . $value . '%'];
        //     } else {
        //         $where[] = ['test_rich', 'like', '%' . $value . '%'];
        //     }
        // }
        // if ($test_reference == 1 || $test_reference == 0) {
        //     $where[] = ['test_reference', '=', $test_reference];
        // }
        // 分页 第p页
        $p = input('post.p', 1);
        // 分页 每页显示的条数
        $num = 2;
        // 分页
        $total = Db::table('app_shop_goods_info')->where("is_del", 1)->count();
        // $lists = Db::table('app_shop_goods_info')->order('sort DESC')->where("is_del", 1)->page($p, $num)->select()->toArray();
        $lists = Db::table('app_shop_goods_info')->order('sort DESC')->where("is_del", 1)->select()->toArray();
        if (empty($lists)) {
            $this->returnCode(90000203);
        }
        $types = Db::table('app_shop_goods_type')->order('type_id ASC')->where(['is_del' => 1, 'status' => 1])->select()->toArray();

        // 处理数据 返回给前端显示
        foreach ($lists as &$lists_v) {
            $lists_v['status_s'] = $lists_v['status'] == 1 ? '上架' : '下架';
            $lists_v['is_top_s'] = $lists_v['is_top'] == 1 ? '默认' : '置顶';
            $lists_v['time_add'] = date('Y年m月d日 H:m:s', $lists_v['time_add']);
            $lists_v['time_update'] = $lists_v['time_update'] == null ? '' : date('Y年m月d日 H:m:s', $lists_v['time_update']);
            // $lists_v['img_s'] = [];
            // 遍历数组 并将图片路径设置为绝对路径 方便前端展示
            if (!empty($lists_v['img'])) {
                // 将字符串以 ；符号分割为数组
                $img =  explode(';', $lists_v['img']);
                // 弹出数组中最后一个元素 因为以 ；分隔为数组后最后一个元素为空值
                array_pop($img);
                foreach ($img as $k => $img_v) {
                    if (!empty($img_v)) {
                        $lists_v['img_s'][] = [
                            'uid' => $k,
                            'url' => $this->domain_name . $img_v,
                            'data' => $img_v
                        ];
                    }
                }
            }
        }

        $this->returnCode(0, ['lists' => $lists, 'types' => $types, 'total' => $total]);
    }

    # 商品信息添加、修改
    public function goodssave()
    {
        $gid = (int)trim(input('post.gid'));
        $data['title'] = trim(input('post.title'));
        $data['desc'] = trim(input('post.desc'));
        $data['type_id'] = (int)trim(input('post.type_id'));
        $data['oprice'] = trim(input('post.oprice'));
        $data['nprice'] = trim(input('post.nprice'));
        $data['status'] = (int)trim(input('post.status'));
        $data['is_top'] = trim(input('post.is_top'));
        $data['sort'] = trim(input('post.sort'));
        $data['num'] = trim(input('post.num'));
        $data['content'] = trim(input('post.content'));

        // 将img转成字符串存于数据库中 以 ；分割
        $img = input('post.img');
        $img_str = '';
        foreach ($img as $v) {
            $img_str = $img_str . $v . ';';
        }
        $data['img'] = $img_str;
        // 后端的简单校验
        if (!$data['title'])  $this->returnCode(90000204);
        if (!$data['img'])  $this->returnCode(90000205);
        if (!$data['content'])  $this->returnCode(90000206);
        if (!$data['type_id'] || $data['type_id'] < 1)  $this->returnCode(90000207);

        if (empty($gid) || $gid === 0) {
            $data['time_add'] = time();
            // 销量默认为0 
            $data['sales'] = 0;
            $res = Db::table('app_shop_goods_info')->insert($data);
            if (!$res) {
                $this->returnCode(91000001);
            }
            $this->returnCode(0);
        } else {
            $data['time_update'] = time();
            $res = Db::table('app_shop_goods_info')->where('gid', $gid)->update($data);
            if (!$res) {
                $this->returnCode(91000002);
            }
            // 遍历数组 并将图片路径设置为绝对路径 方便前端展示
            if (!empty($data['img'])) {
                // 将字符串以 ；符号分割为数组
                $img =  explode(';', $data['img']);
                // 弹出数组中最后一个元素 因为以 ；分隔为数组后最后一个元素为空值
                array_pop($img);
                foreach ($img as $k => $img_v) {
                    if (!empty($img_v)) {
                        $data['img_s'][] = [
                            'uid' => $k,
                            'url' => $this->domain_name . $img_v,
                            'data' => $img_v
                        ];
                    }
                }
            }
            $data['gid'] = $gid;
            $this->returnCode(0, $data);
        }
    }

    # 商品信息伪删除
    public function goodsdel()
    {
        $gid = (int)input('post.gid');
        $data['is_del'] = 0;
        $res = Db::table('app_shop_goods_info')->where('gid', $gid)->update($data);
        if (empty($res)) {
            $this->returnCode(91000003);
        }
        $this->returnCode(0);
    }

    # 商品分类列表
    public function typelists()
    {
        $lists = Db::table('app_shop_goods_type')->where("is_del", "=", 1)->select()->toArray();
        if (empty($lists)) {
            $this->returnCode(90000201);
        }
        foreach ($lists as &$lists_v) {
            $lists_v['key'] = $lists_v['type_id'];
            if ($lists_v['status'] == 1) {
                $lists_v['status_s'] = '开启';
            } else {
                $lists_v['status_s'] = '关闭';
            }
            $lists_v['time_add'] = date('Y年m月d日', $lists_v['time_add']);
        }
        $this->returnCode(0, $lists);
    }

    # 商品分类添加、修改
    public function typesave()
    {
        $type_id = (int)trim(input('post.type_id'));
        $data['name'] = trim(input('post.name'));
        $data['desc'] = trim(input('post.desc'));
        if (!$data['name']) {
            $this->returnCode(90000202);
        }
        $data['status'] = (int)trim(input('post.status'));

        if (empty($type_id) || $type_id === 0) {
            $data['time_add'] = time();
            $res = Db::table('app_shop_goods_type')->insert($data);
            if (!$res) {
                $this->returnCode(91000001);
            }
            $this->returnCode(0);
        } else {
            $data['time_update'] = time();
            $res = Db::table('app_shop_goods_type')->where('type_id', $type_id)->update($data);
            if (!$res) {
                $this->returnCode(91000002);
            }
            $data['dep_id'] = $type_id;
            $this->returnCode(0, $data);
        }
    }

    # 商品分类伪删除
    public function typedel()
    {
        $type_id = (int)input('post.type_id');
        $data['is_del'] = 0;
        $res = Db::table('app_shop_goods_type')->where('type_id', $type_id)->update($data);
        if (empty($res)) {
            $this->returnCode(91000003);
        }
        $this->returnCode(0);
    }

    # 订单列表
    public function orderLists()
    {
    }

    # 新增订单和修改订单
    public function orderSave()
    {
    }

    # 取消订单
    public function orderCancel()
    {
    }
}
